package com.hiibase.core.security.entity;


import com.alibaba.fastjson.annotation.JSONField;
import com.hibase.common.entity.User;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.UserDetails;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class UserDetail implements Serializable, UserDetails {
    private static final long serialVersionUID = 1L;

    private User user;

    private List<String> roles;

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return AuthorityUtils
                .createAuthorityList(roles.toArray(new String[0]));
    }

    @Override
    public String getPassword() {
        return this.user.getLoginPwd();
    }

    @Override
    public String getUsername() {
        return this.user.getLoginName();
    }

    /**
     * 账户是否没过期
     *
     * @return boolean
     */
    @Override
    @JSONField(serialize = false,deserialize = false)
    public boolean isAccountNonExpired() {
        return true;
    }

    /**
     * 账户是否没被锁定
     *
     * @return boolean
     */
    @Override
    @JSONField(serialize = false,deserialize = false)
    public boolean isAccountNonLocked() {
        return true;
    }

    /**
     * 账户凭据是否没过期
     *
     * @return boolean
     */
    @Override
    @JSONField(serialize = false,deserialize = false)
    public boolean isCredentialsNonExpired() {
        return true;
    }

    /**
     * 账户是否启用
     *
     * @return boolean
     */
    @Override
    @JSONField(serialize = false,deserialize = false)
    public boolean isEnabled() {
        return true;
    }
}
